package com.webchat.chat.service.impl;

import com.webchat.chat.core.AbstractService;
import com.webchat.chat.core.baseEntity.Result;
import com.webchat.chat.core.baseEntity.ResultGenerator;
import com.webchat.chat.core.enums.UserAccountType;
import com.webchat.chat.core.exception.ServiceException;
import com.webchat.chat.core.tools.ShiroUtil;
import com.webchat.chat.dao.PermissionMapper;
import com.webchat.chat.dao.RoleMapper;
import com.webchat.chat.dao.UserRoleMapper;
import com.webchat.chat.model.Permission;
import com.webchat.chat.model.Role;
import com.webchat.chat.model.UserRole;
import com.webchat.chat.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * Created by admin on 2018-06-29.
 */
@Service
@Transactional
public class RoleServiceImpl extends AbstractService<Role> implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public List<Role> findRolesByUserId(String userId) {
        return roleMapper.findRolesByUserId(userId);
    }

    @Override
    public List<String> findRoleNamesByUserId(String userId) {
        return roleMapper.findRoleNamesByUserId(userId);
    }



    @Override
    public void save(Role role) {
        String tenantCode = role.getTenantCode();
        String roleName = role.getRoleName();
        Role roleCondition = new Role();
        roleCondition.setTenantCode(tenantCode);
        roleCondition.setRoleName(roleName);
        List<Role> selectRole = roleMapper.select(roleCondition);
        if(selectRole != null && !selectRole.isEmpty()){
            throw new ServiceException("已存在此角色名,请更换角色名重试");
        }
        super.save(role);
    }

    @Override
    public void deleteById(String id) {
        UserRole userRoleCondition = new UserRole();
        userRoleCondition.setRoleId(id);
        List<UserRole> selectUserRole = userRoleMapper.select(userRoleCondition);
        if(selectUserRole != null && !selectUserRole.isEmpty()){
            throw new ServiceException("此角色已关联用户,请解除关联后重试");
        }
        super.deleteById(id);
    }




    public Result checkRole(Role role) {
        UserAccountType roleType = role.getRoleType();
        String roleName = role.getRoleName();
        if(roleType == null){
            return ResultGenerator.genFailResult("角色类型必填");
        }
        if(StringUtils.isBlank(roleName)){
            return ResultGenerator.genFailResult("角色名必填");
        }
        role.setTenantCode(ShiroUtil.getTenantCode());
        Date now = new Date();
        role.setCreatedBy(ShiroUtil.getUserName());
        role.setCreatedDate(now);
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Role findDetailById(String roleId) {
        Role role = super.findById(roleId);
        List<Permission> permissionList ;
        if (StringUtils.isNotBlank(roleId)){
            permissionList=permissionMapper.findPermessionByRoleId(roleId);
            if (!permissionList.isEmpty()){
                role.setPermissions(permissionList);
            }
        }
        return role;
    }

    @Override
    public List<Role> findRoleByInfo(Role role) {
        List<Role> roleList= roleMapper.findRoleByInfo(role);
        return roleList;
    }

    @Override
    public Role saveAndReturnRole(Role role) {
        String tenantCode = role.getTenantCode();
        String roleName = role.getRoleName();
        Role roleCondition = new Role();
        roleCondition.setTenantCode(tenantCode);
        roleCondition.setRoleName(roleName);
        List<Role> selectRole = roleMapper.select(roleCondition);
        if(selectRole != null && !selectRole.isEmpty()){
            throw new ServiceException("已存在此角色名,请更换角色名重试");
        }
        super.save(role);
        return  role;
    }

    @Override
    public List<Role> findAllByTenantCode() {
        Role role = new Role();
        if(!"All".equals(ShiroUtil.getTenantCode())){
            role.setTenantCode(ShiroUtil.getTenantCode());
        }
        return roleMapper.select(role);
    }
}
